<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * This is a PHP library that handles cURL in CodeIgniter
 *
* @package     CodeIgniter
* @subpackage  Libraries
* @category    Olycurl
* @author      Stephane Kouakou <stephane@ollyworks.com>
*/

// ------------------------------------------------------------------------

/**
 * Ollycurl Class
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Ollycurl
 * @author		Ollyworks
 * @link		http://www.ollyworks.com/
 */
class Ollycurl {


				var $cookies;
				var $header;
				var $return_transfer;
				var $referer;
				var $connection_timeout;
				var $timeout;
				var $user_agent;
				var $auto_follow;
				var $output;
				var $curl_infos;
				var $verbose;
				var $ssl_verif_peer;
				var $ssl_verif_host;
				var $proxy_ip;
				var $proxy_port;
				var $proxy_auth;
				var $proxy_user;
				var $proxy_pass;
				var $http_user;
				var $http_pass;
				var $headers;
				var $header_accept;
				var $header_lang;
				var $header_encoding;
				var $header_charset;
				var $header_keepalive;
				var $header_connection;
				var $port;
				var $config_cache = array();
				var $verbose_file_handling_type;

				function __construct()
				{
						$this->cookies = './cookies.txt';
						$this->timeout = 120;
						$this->connection_timeout = 60;
						$this->header = 1;
						$this->return_transfer = 1;
						$this->auto_follow = false;
						$this->user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14';
						$this->ssl_verif_peer = false;
						$this->ssl_verif_host = false;
						$this->proxy_auth = CURLAUTH_BASIC;
						$this->header_accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
						$this->header_lang = 'en-us,en;q=0.5';
						$this->header_charset = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7';
						$this->header_keepalive = '300';
						$this->header_connection = 'keep-alive';
						$this->verbose = true;
						$this->verbose_file_handling_type = 'w';
						$this->verbose_file = './http_data.txt';
						$this->port = '';
				}
				
				function save_config_cache(){
						$this->config_cache['cookies'] 				= $this->cookies;
						$this->config_cache['timeout'] 				= $this->timeout;
						$this->config_cache['connection_timeout'] 	= $this->connection_timeout;
						$this->config_cache['header'] 				= $this->header;
						$this->config_cache['return_transfer'] 		= $this->return_transfer;
						$this->config_cache['referer'] 				= $this->referer;
						$this->config_cache['auto_follow'] 			= $this->auto_follow;
						$this->config_cache['user_agent'] 			= $this->user_agent;
						$this->config_cache['ssl_verif_peer'] 		= $this->ssl_verif_peer;
						$this->config_cache['ssl_verif_host'] 		= $this->ssl_verif_host;
						$this->config_cache['proxy_auth'] 			= $this->proxy_auth;
						$this->config_cache['header_accept'] 		= $this->header_accept;
						$this->config_cache['header_lang'] 			= $this->header_lang;
						$this->config_cache['header_charset'] 		= $this->header_charset;
						$this->config_cache['header_keepalive'] 	= $this->header_keepalive;
						$this->config_cache['header_connection'] 	= $this->header_connection;
						$this->config_cache['verbose'] 				= $this->verbose;
						$this->config_cache['verbose_file'] 		= $this->verbose_file;
						$this->config_cache['port'] 				= $this->port;
						$this->config_cache['headers'] 				= $this->headers;
				}
				
				function restore_config_cache(){
						$this->cookies 				= $this->config_cache['cookies'];
						$this->timeout 				= $this->config_cache['timeout'];
						$this->connection_timeout 	= $this->config_cache['connection_timeout'];
						$this->header 				= $this->config_cache['header'];
						$this->return_transfer 		= $this->config_cache['return_transfer'];
						$this->referer 				= $this->config_cache['referer'];
						$this->auto_follow 			= $this->config_cache['auto_follow'];
						$this->user_agent 			= $this->config_cache['user_agent'];
						$this->ssl_verif_peer 		= $this->config_cache['ssl_verif_peer'];
						$this->ssl_verif_host 		= $this->config_cache['ssl_verif_host'];
						$this->proxy_auth 			= $this->config_cache['proxy_auth'];
						$this->header_accept 		= $this->config_cache['header_accept'];
						$this->header_lang 			= $this->config_cache['header_lang'];
						$this->header_charset 		= $this->config_cache['header_charset'];
						$this->header_keepalive 	= $this->config_cache['header_keepalive'];
						$this->header_connection 	= $this->config_cache['header_connection'];
						$this->verbose 				= $this->config_cache['verbose'];
						$this->verbose_file 		= $this->config_cache['verbose_file'];
						$this->port 				= $this->config_cache['port'];
						$this->headers 				= $this->config_cache['headers'];
				}
				
				function restore_default_config(){
						$this->cookies = './cookies.txt';
						$this->timeout = 120;
						$this->connection_timeout = 60;
						$this->header = 1;
						$this->return_transfer = 1;
						$this->referer = '';
						$this->auto_follow = false;
						$this->user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14';
						$this->ssl_verif_peer = false;
						$this->ssl_verif_host = false;
						$this->proxy_auth = CURLAUTH_BASIC;
						$this->header_accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
						$this->header_lang = 'en-us,en;q=0.5';
						$this->header_charset = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7';
						$this->header_keepalive = '300';
						$this->header_connection = 'keep-alive';
						$this->verbose = true;
						$this->verbose_file = './http_data.txt';
						$this->port = '';
						$this->headers = array();
				}
				
				function set_headers()
				{
						if($this->header_accept) 			$this->headers[] = "Accept: " . $this->header_accept;
						if($this->header_lang) 				$this->headers[] = "Accept-Language: " . $this->header_lang;
						if($this->header_charset) 			$this->headers[] = "Accept-Charset: " . $this->header_charset;
						if($this->header_keepalive) 		$this->headers[] = "Keep-Alive: " . $this->header_keepalive;
						if($this->header_connection) 		$this->headers[] = "Connection: " . $this->header_connection;
						
						$this->headers = array_unique($this->headers);
				}
				
				function request($url, $method = 'get', $postdata = '')
				{
						$ch = curl_init();
						curl_setopt($ch, CURLOPT_URL, $url);
						if($this->verbose) 
						{
								curl_setopt($ch, CURLOPT_VERBOSE, 1);
								$ext = strtolower(pathinfo($this->verbose_file, PATHINFO_EXTENSION));
								$hndt = $ext=='txt'?'a':'b';
								if($fp = fopen($this->verbose_file, $this->verbose_file_handling_type.$hndt."+")) curl_setopt($ch, CURLOPT_STDERR, $fp);
						}
						if($method == 'post')
						{
								curl_setopt($ch, CURLOPT_POST, 1);
								curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
						}

						curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connection_timeout);
						curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
						curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
						if( substr(strtolower($url), 0, 5) == 'https' )
						{
								curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->ssl_verif_peer);
								curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->ssl_verif_host);
						}
						if($this->cookies)
						{
								curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookies);
								curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookies);
						}
						curl_setopt($ch, CURLOPT_HEADER, $this->header);
						curl_setopt($ch, CURLOPT_RETURNTRANSFER, $this->return_transfer); 
						if($this->auto_follow)
						{
								curl_setopt($ch, CURLOPT_AUTOREFERER, true);
								curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->auto_follow);
						}
						if($this->referer) curl_setopt($ch, CURLOPT_REFERER, $this->referer);
						if($this->proxy_ip && $this->proxy_port)
						{
								curl_setopt($ch, CURLOPT_PROXY, $this->proxy_ip . ":" . $this->proxy_port);
								if($this->proxy_user && $this->proxy_pass)
								{
										curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->proxy_user . ":" . $this->proxy_pass);
								}
						}
						if($this->http_user && $this->http_pass)
						{
								curl_setopt($ch, CURLOPT_USERPWD, $this->http_user . ":" . $this->http_pass);
						}
						$this->set_headers();
						if(count($this->headers)>0) curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
						if($this->port) curl_setopt($ch, CURLOPT_PORT, $this->port); 
						$this->output = curl_exec($ch);
						$this->curl_infos = curl_getinfo($ch);
						curl_close($ch);
						if($fp) fclose($fp);
						return $this->output;
				}
								
				function post($url, $postdata = '')
				{
						return $this->request($url, 'post', $postdata);
				}
				
				function get($url)
				{
						return $this->request($url);
				}
				
				function clean_nl()
				{
						return str_replace( array("\r\n", "\n"), '', $this->output);
				}
				
				function parse_string($pattern, $string)
				{
						preg_match_all($pattern, $string, $matches);
						return $matches;
				}

}


// END Ollycurl Class

/* End of file ollycurl.php */
/* Location: ./system/application/libraries/ollycurl.php */